package com.yitiao.data_algorithms.leetcode.editor.cn;

import com.yitiao.data_algorithms.leetcode.editor.cn.base.ListNode;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.TreeNode;

import java.util.HashSet;
import java.util.Set;

class ContainsDuplicateIi {
    // 2022-08-22 13:09:22
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean containsNearbyDuplicate(int[] nums, int k) {
            // 窗口最大长度为 k
            int n = nums.length;
            int left = 0, right = 0;
            Set<Integer> set = new HashSet<>();
            while (right < n) {
                while (right - left > k) {
                    set.remove(nums[left]);
                    left++;
                }
                if (!set.add(nums[right])) {
                    return true;
                }
                right++;
            }
            return false;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}